package com.mixes;

import static com.mixes.util.Utility.*;

import java.util.HashMap;
import java.util.LinkedList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lib.quickaction.ActionItem;
import com.lib.quickaction.QuickAction;
import com.mixes.adapter.FacebookAdapter;
import com.mixes.consts.FacebookConsts;
import com.mixes.field.IField;
import com.mixes.logic.FacebookLogic;
import com.mixes.logic.ILogic.OnAuthorizeListener;
import com.mixes.logic.ILogic.OnTimeLineListener;

/**
 * TODO Auto-generated constructor stub
 * @author Author:
 * @version Revision:
 */
public class StreamActivity extends BaseActivity {
    private static final String TAG = "StreamActivity";

    private PullToRefreshListView mStreamView;

    // private TwitterLogic mTwitterLogic;

    // private TwitterAdapter mTwitterAdapter;

    private FacebookLogic mFacebookLogic;

    private FacebookAdapter mFacebookAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stream);

        mStreamView = customFindViewById(this, R.id.stream_view);
        mStreamView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                QuickAction quickAction = new QuickAction(view);
                ActionItem actionItem = new ActionItem();
                actionItem.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
                actionItem.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showToast("Clicked");
                    }
                });
                quickAction.addActionItem(actionItem);
                quickAction.show();
            }
        });

        // mTwitterLogic = TwitterLogic.getInstance(this, TwitterConsts.CALLBACK, TwitterConsts.CONSUMER_KEY, TwitterConsts.CONSUMER_SECRET);
        // mTwitterLogic.authorize(new OnAuthorizeListener() {
        // @Override
        // public void onStart() {
        // showDebugToast("onStart");
        // }
        //
        // @Override
        // public void onFailed(Exception e) {
        // showDebugToast("onFailed");
        // }
        //
        // @Override
        // public void onComplete() {
        // showDebugToast("onComplete");
        //
        // setTwitterTimeLine();
        // }
        // });

        // mFacebookLogic = new FacebookLogic(this, FacebookConsts.APP_ID);
        mFacebookLogic = FacebookLogic.getInstance(this, FacebookConsts.APP_ID);
        mFacebookLogic.authorize(new OnAuthorizeListener() {
            @Override
            public void onStart() {
                showDebugToast("onStart");
            }

            @Override
            public void onFailed(Exception e) {
                showDebugToast("onFailed");
            }

            @Override
            public void onComplete() {
                showDebugToast("onComplete");
                setFacebookTimeLine();
            }
        });

        mStreamView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // setTwitterTimeLine();
                setFacebookTimeLine();
            }
        });

        mOnMenuClickListener = new OnMenuClickListener() {
            @Override
            public void onClickSend() {
                // TODO 送信画面を起動する

                // mTwitterLogic.send("これはテストです", new OnSendListener() {
                // @Override
                // public void onStart() {
                // showDebugToast("onStart");
                // }
                //
                // @Override
                // public void onFailed() {
                // showDebugToast("onFailed");
                // }
                //
                // @Override
                // public void onComplete() {
                // setTwitterTimeLine();
                // showDebugToast("onComplete");
                // }
                // });
            }

            @Override
            public void onClickSearch() {
            }
        };
    }

    // private void setTwitterTimeLine() {
    // mTwitterLogic.prepareTimeLine(new OnTimeLineListener() {
    // @Override
    // public void onStart() {
    // showDebugToast("onStart");
    // }
    //
    // @Override
    // public void onFailed(Exception e) {
    // mStreamView.onRefreshComplete();
    // showDebugToast("onFailed");
    // }
    //
    // @Override
    // public void onComplete(LinkedList<HashMap<IField, Object>> list) {
    // mStreamView.onRefreshComplete();
    // mTwitterAdapter = new TwitterAdapter(getApplicationContext(), list);
    // mStreamView.setAdapter(mTwitterAdapter);
    // showDebugToast("onComplete");
    // }
    // });
    // }

    private void setFacebookTimeLine() {
        mFacebookLogic.prepareTimeLine(new OnTimeLineListener() {
            @Override
            public void onStart() {
                showDebugToast("onStart");
            }

            @Override
            public void onFailed(Exception e) {
                showDebugToast("onFailed");
            }

            @Override
            public void onComplete(LinkedList<HashMap<IField, Object>> list) {
                mStreamView.onRefreshComplete();
                mFacebookAdapter = new FacebookAdapter(getApplicationContext(), list);
                mStreamView.setAdapter(mFacebookAdapter);
                showDebugToast("onComplete");
            }
        });
    }
}
